<template>
  <div>
    <el-dialog
      v-model="open"
      :close-on-click-modal="false"
      title="添加品名信息"
    >
      <!-- 内容主体 -->
      <el-form
        ref="formRef"
        :model="form"
        label-position="left"
        label-width="121px"
        inline
        :rules="rules"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="HS编码" prop="hsCode" :label-position="labelPosition">
              <el-input v-model="form.hsCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文品名" prop="productNameEn" :label-position="labelPosition">
              <el-input v-model="form.productNameEn"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="品名中文名称" prop="productName" :label-position="labelPosition">
              <el-input v-model="form.productName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="材质英文名称" prop="materialEn" :label-position="labelPosition">
              <el-input v-model="form.materialEn"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="材质" prop="material" :label-position="labelPosition">
              <el-input v-model="form.material"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用途英文名称" prop="categoryUseEn" :label-position="labelPosition">
              <el-input v-model="form.categoryUseEn"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用途" prop="categoryUse" :label-position="labelPosition">
              <el-input v-model="form.categoryUse"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产商英文名称" prop="manufacturerEn" :label-position="labelPosition">
              <el-input v-model="form.manufacturerEn"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产商" prop="manufacturer" :label-position="labelPosition">
              <el-input v-model="form.manufacturer"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="原产地英文名称"
              prop="placeOriginEn"
              :label-position="labelPosition"
            >
              <el-input v-model="form.placeOriginEn"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="原产地" prop="placeOrigin" :label-position="labelPosition">
              <el-input v-model="form.placeOrigin"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图片" :label-position="labelPosition" prop="">
              <image-upload></image-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span>
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, getCurrentInstance, watch, defineEmits, defineExpose } from 'vue'
import { ElMessage } from 'element-plus'
import useDicts, { DictDirection } from '@/store/modules/dicts'
import { addData, editData, getDatail } from '@/api/BusinessConfig/articleNamemaintain'

const dicts = useDicts()
dicts.setDicts(DictDirection.是否禁用字典)
// 表单参数
let
  form = ref({
    id: undefined,
    hsCode: undefined,
    productName: undefined,
    productNameEn: undefined,
    material: undefined,
    materialEn: undefined,
    categoryUse: undefined,
    categoryUseEn: undefined,
    manufacturer: undefined,
    manufacturerEn: undefined,
    placeOrigin: undefined,
    placeOriginEn: undefined,
  }),
  // 标题
  title = ref(''),
  // 模态框显示
  open = ref(false),
  labelPosition = ref('right'),
  rules = {
    hsCode: [{ required: true, message: '必填', trigger: 'blur' }],
    productName: [{ required: true, message: '必填', trigger: 'blur' }],
    productNameEn: [{ required: true, message: '必填', trigger: 'blur' }],
    material: [{ required: true, message: '必填', trigger: 'blur' }],
    materialEn: [{ required: true, message: '必填', trigger: 'blur' }],
    categoryUse: [{ required: true, message: '必填', trigger: 'blur' }],
    categoryUseEn: [{ required: true, message: '必填', trigger: 'blur' }],
    manufacturer: [{ required: true, message: '必填', trigger: 'blur' }],
    manufacturerEn: [{ required: true, message: '必填', trigger: 'blur' }],
    placeOrigin: [{ required: true, message: '必填', trigger: 'blur' }],
    placeOriginEn: [{ required: true, message: '必填', trigger: 'blur' }],
  },
  // 新增修改表单
  editForm = ref(null),
  {
    appContext: {
      config: {
        globalProperties: { $resetForm, $validateForm },
      },
    },
  } = getCurrentInstance()
const emit = defineEmits(['changeList'])
// 重置新增修改列表
const reset = () => {
  form.value = {
    id: undefined,
    hsCode: undefined,
    productName: undefined,
    productNameEn: undefined,
    material: undefined,
    materialEn: undefined,
    categoryUse: undefined,
    categoryUseEn: undefined,
    manufacturer: undefined,
    manufacturerEn: undefined,
    placeOrigin: undefined,
    placeOriginEn: undefined,
  }
  $resetForm(editForm.value)
}
// 新增修改——确定
const submitForm = () => {
  $validateForm(editForm.value, () => {
    let msg = ''
    let response = null
    if (form.value.id != undefined) {
      msg = '修改成功'
      response = editData(form.value)
    } else {
      msg = '新增成功'
      response = addData(form.value)
    }
    response
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success(msg)
        open.value = false
        emit('changeList')
      } else {
        ElMessage.error(res.msg)
      }
    })
    .catch((error) => {
      ElMessage.error(error)
    })
  })
}
// 修改新增——取消
const cancel = () => {
  open.value = false
}
const getList = (id) => {
  getDatail(id)
  .then((res) => {
    if (res.code === 200) {
      form.value = res.data
    } else {
      ElMessage.error(res.msg)
    }
  })
  .catch((err) => {
    console.log(err)
    ElMessage.error(err)
  })
}

watch(open, (v) => {
  if (!v) {
    reset()
  }
})

defineExpose({
  open,
  title,
  getList,

})
</script>